package com.yanghui.controller.admin;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.yanghui.pojo.Visit;
import com.yanghui.service.VisitService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@SuppressWarnings("all")
@RequestMapping("/admin")
@Controller
public class VisitControllerAdmin {
    @Resource(name = "visitServiceImpl")
    private VisitService visitService;


    /**
     * 按照指定日期查询访客记录
     */
    @RequestMapping("/selectVisitListByDate")
    @ResponseBody
    public Map<String,Object> selectVisitListByDate(
            @RequestParam("startTime") String startTime,
            @RequestParam("endTime") String endTime,
            @RequestParam("format") String format) {
        Map<String,Object> map = new HashMap<>();
        if (startTime != null && "".equals(startTime) != true) {
            map.put("startTime", startTime);
        }
        if (endTime != null && "".equals(endTime) != true) {
            map.put("endTime", endTime);
        }
        if (format != null && "".equals(format) != true) {
            map.put("format", format);
        }

        List<?> list = visitService.selectVisitListByDate(map);
        Map<String,Object> resultMap = new HashMap<>();

        if (list.size() > 0) {
            resultMap.put("status", 200);
        } else {
            resultMap.put("status", 500);
        }
        resultMap.put("list", list);

        return resultMap;
    }

    /**
     * 按照ID删除访问记录
     */
    @RequestMapping("/deleteVisitByID")
    @ResponseBody
    public Map<String,Object> deleteVisitByID(@RequestParam("id") Integer id) {
        Map<String,Object> map = new HashMap<>();
        int count = visitService.deleteVisitByID(id);

        if (count == 1) {
            map.put("status", 200);
        } else {
            map.put("status", 500);
        }

        return map;
    }

    /**
     * 按照IP分组
     */
    @RequestMapping("/selectLikeVisitListGroupByIp")
    @ResponseBody
    public Map<String,Object> selectLikeVisitListGroupByIp(
            Visit visit,
            @RequestParam(value = "page", required = true, defaultValue = "1") Integer page,
            @RequestParam(value = "pageSize", required = true, defaultValue = "10") Integer pageSize,
            @RequestParam("startTime") String startTime,
            @RequestParam("endTime") String endTime) {
        /** 封装要查询的条件 */
        Map<String,Object> map = new HashMap<>();
        if (visit.getIp() != null && "".equals(visit.getIp()) != true) {
            map.put("ip", visit.getIp());
        }
        if (visit.getCity() != null && "".equals(visit.getCity()) != true) {
            map.put("city", visit.getCity());
        }
        if (visit.getPlatformType() != null && "".equals(visit.getPlatformType()) != true) {
            map.put("platformType", visit.getPlatformType());
        }
        if (visit.getBrowserType() != null && "".equals(visit.getBrowserType()) != true) {
            map.put("browserType", visit.getBrowserType());
        }
        if (startTime != null && "".equals(startTime) != true) {
            map.put("startTime", startTime);
        }
        if (endTime != null && "".equals(endTime) != true) {
            map.put("endTime", endTime);
        }


        /** 使用 PageHelper 插件进行分页 */
        PageHelper.startPage(page, pageSize);
        List<?> visitList = visitService.selectLikeVisitListGroupByIp(map);
        PageInfo pageInfo = new PageInfo(visitList);
        Map<String,Object> resultMap = new HashMap<>();

        if (visitList.size() > 0) {
            resultMap.put("status", 200);
        } else {
            resultMap.put("status", 500);
        }
        resultMap.put("pageInfo", pageInfo);

        return resultMap;
    }

    /**
     * 按照指定的条件查询访问者数据
     */
    @RequestMapping("/selectLikeVisitListByPage")
    @ResponseBody
    public Map<String,Object> selectLikeVisitListByPage(
            Visit visit,
            @RequestParam(value = "page", required = true, defaultValue = "1") Integer page,
            @RequestParam(value = "pageSize", required = true, defaultValue = "10") Integer pageSize,
            @RequestParam("startTime") String startTime,
            @RequestParam("endTime") String endTime) {
        /** 封装要查询的条件 */
        Map<String,Object> map = new HashMap<>();
        if (visit.getIp() != null && "".equals(visit.getIp()) != true) {
            map.put("ip", visit.getIp());
        }
        if (visit.getCity() != null && "".equals(visit.getCity()) != true) {
            map.put("city", visit.getCity());
        }
        if (visit.getPlatformType() != null && "".equals(visit.getPlatformType()) != true) {
            map.put("platformType", visit.getPlatformType());
        }
        if (visit.getBrowserType() != null && "".equals(visit.getBrowserType()) != true) {
            map.put("browserType", visit.getBrowserType());
        }
        if (startTime != null && "".equals(startTime) != true) {
            map.put("startTime", startTime);
        }
        if (endTime != null && "".equals(endTime) != true) {
            map.put("endTime", endTime);
        }


        /** 使用 PageHelper 插件进行分页 */
        PageHelper.startPage(page, pageSize);
        List<Visit> visitList = visitService.selectLikeVisitListByPage(map);
        PageInfo pageInfo = new PageInfo(visitList);
        Map<String,Object> resultMap = new HashMap<>();

        if (visitList.size() > 0) {
            resultMap.put("status", 200);
        } else {
            resultMap.put("status", 500);
        }
        resultMap.put("pageInfo", pageInfo);

        return resultMap;
    }

    /**
     * 按照IP分组查询 用户数量以及查询总记录数
     */
    @RequestMapping("/selectVisitListByIp")
    @ResponseBody
    public Map<String,Object> selectVisitListByIp() {
        Map<String,Object> map = new HashMap<>();
        List<?> list = visitService.selectVisitListByIp();
        map.put("list", list);

        return map;
    }
}
